package com.summerxia.dateselector.widget;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.summerxia.dateselector.R;
import com.summerxia.dateselector.animation.Effectstype;
import com.summerxia.dateselector.utils.DateUtils;

import java.util.List;


public class NewsCompontentSelectorDialogBuilder extends NiftyDialogBuilder implements
        android.view.View.OnClickListener {

    private Context context;
    private RelativeLayout rlCustomLayout;
    private NewsCompontentWheel newsCompontentWheel;
    private OnSaveLocationLister saveLocationLister;
    private static NewsCompontentSelectorDialogBuilder instance;
    private static int mOrientation = 1;

    public interface OnSaveLocationLister {
        abstract void onSaveLocation(String location, String provinceId, String cityId);
    }

    public NewsCompontentSelectorDialogBuilder(Context context, int theme) {
        super(context, theme);
        this.context = context;
        initDialog();
    }

    public NewsCompontentSelectorDialogBuilder(Context context) {
        super(context);
        this.context = context;
        initDialog();
    }

    public static NewsCompontentSelectorDialogBuilder getInstance(Context context) {

        int ort = context.getResources().getConfiguration().orientation;
        if (mOrientation != ort) {
            mOrientation = ort;
            instance = null;
        }

        if (instance == null || ((Activity) context).isFinishing()) {
            synchronized (NewsCompontentSelectorDialogBuilder.class) {
                if (instance == null) {
                    instance = new NewsCompontentSelectorDialogBuilder(context, R.style.dialog_untran);
                }
            }
        }
        return instance;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(R.drawable.edit_dialog_coner);
    }

    private void initDialog() {
        rlCustomLayout = (RelativeLayout) LayoutInflater.from(context).inflate(
                R.layout.compontent_selector_dialog_layout, null);
        newsCompontentWheel = (NewsCompontentWheel) rlCustomLayout
                .findViewById(R.id.aw_location_selector_wheel);
        setDialogProperties();
    }

    private void setDialogProperties() {
        int width = DateUtils.getScreenWidth(context);
        this.withDialogWindows(width, LayoutParams.WRAP_CONTENT)
                .withTitleColor("#000000")
                .setDialogClick(this).withEffect(Effectstype.Slit)
                .withIcon(R.drawable.choose)
                .withDuration(200).setPreviousLayoutClick(this)
                .withNextImage(R.drawable.choose_sure)
                .withMessageMiss(View.GONE).withNextTextColor("#3598da")
                .setNextLayoutClick(this)
                .setCustomView(rlCustomLayout, context);
    }

    public void setNewsCompontents(List<String> newsCompontents) {
        this.withTitle("选择组件分类");
        newsCompontentWheel.setNewsCompontents(newsCompontents);
    }

    public void setClassifys(List<String> classifys, int num) {
        newsCompontentWheel.setClassifys(classifys, num);
    }


    public void setOnlyClassify(String type) {
        if (type.equals("news") || type.equals("channel")) {
            this.withTitle("选择分类");
        } else if (type.equals("imgtextmsg")) {
            this.withTitle("选择消息人");
        } else if (type.equals("modern")) {
            this.withTitle("选择推流地址");
        } else if ("deparment".equals(type)) {
            this.withTitle("选择部门");
        }
        newsCompontentWheel.setOnlyClassify();
    }

    public NewsCompontentWheel getNewsCompontentWheel() {
        return newsCompontentWheel;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.fl_dialog_title_previous) {
            dismiss();
        } else if (id == R.id.fl_dialog_title_next) {
            if (null != saveLocationLister) {
                saveLocationLister.onSaveLocation(newsCompontentWheel.getNewsName(), newsCompontentWheel.getCompontentId(), newsCompontentWheel.getClassifyId());
            }
            dismiss();
        }
    }

    /**
     * 设置点击保存的监听
     *
     * @param saveLocationLister
     */
    public void setOnSaveLocationLister(OnSaveLocationLister saveLocationLister) {
        this.saveLocationLister = saveLocationLister;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        instance = null;
    }


}
